package com.nine.action;

import java.sql.Timestamp;

import com.nine.IService.CustomerService;
import com.nine.entity.Customer;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.Authenticator.Failure;

public class LoginAction extends ActionSupport {

	String name;
	String password;
	String customerId;

	CustomerService customerService;

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public boolean checkCustomer(String customerId, String password) {
		Customer customer = getCustomerService().findById(customerId);
		if (customer != null) {
			if (customer.getPassword().equals(password))
				return true;
		}
		return false;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		// Customer u = new Customer();
		// u.setCustomerId("104029061@qq.com");
		// u.setCustomerId(name);
		// u.setPassword(password);
		// u.setPoint(0);
		// Timestamp registrationTime = new Timestamp(2011, 6, 15, 20, 20, 20,
		// 10);
		// u.setRegistrationTime(registrationTime);
		// if(customerService.register(u))
		// return SUCCESS;
		// else
		// return ERROR;
		if (checkCustomer(getCustomerId(), getPassword()))
			return SUCCESS;
		else
			return ERROR;

	}

	// public String execute2() throws Exception {
	// // TODO Auto-generated method stub
	// Customer u = new Customer();
	// u.setCustomerId("104029062@qq.com");
	// // //u.setCustomerId(name);
	// u.setPassword(password);
	// // u.setPoint(0);
	// Timestamp registrationTime = new Timestamp(2011, 6, 15, 20, 20, 20, 10);
	// u.setRegistrationTime(registrationTime);
	// if(customerService.register(u))
	// return SUCCESS;
	// else
	// return ERROR;
	// }
}
